// filepath: /home/debi/IdeaProjects/LearnRust/Work/basic/src/corex/cmpx/cmpx.rs
// stdx/cmpx.rs
// CRITICAL: 比较相关示例（Ord, PartialOrd, Eq, PartialEq）。
// AUTO_COMMENT: 展示如何实现比较 trait。

#[allow(dead_code)]
pub fn stdx_examples_cmpx() {
    use std::cmp::Ordering;

    // CRITICAL: 基本比较
    let a = 1;
    let b = 2;
    let ord = a.cmp(&b);
    let partial_ord = a.partial_cmp(&b);

    // CRITICAL: 自定义 PartialEq
    #[derive(Debug)]
    struct Point {
        x: i32,
        y: i32,
    }

    impl PartialEq for Point {
        fn eq(&self, other: &Self) -> bool {
            self.x == other.x && self.y == other.y
        }
    }

    let p1 = Point { x: 1, y: 2 };
    let p2 = Point { x: 1, y: 2 };
    let eq = p1 == p2;

    // CRITICAL: 自定义 Ord
    impl PartialOrd for Point {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }

    impl Ord for Point {
        fn cmp(&self, other: &Self) -> Ordering {
            self.x.cmp(&other.x).then(self.y.cmp(&other.y))
        }
    }

    impl Eq for Point {}

    let cmp_result = p1.cmp(&p2);

    // CRITICAL: 最大最小
    let max_val = a.max(b);
    let min_val = a.min(b);

    let _ = (ord, partial_ord, eq, cmp_result, max_val, min_val);
}
